/**
 * 
 */
package com.hsxc.cdz.mgmt.mobileMessage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hsxc.cdz.base.util.Constants;

import net.sf.json.JSONArray;

/**
 * @author wpfei 短信发送与验证
 */
@Service
public class MobileMessage {

	static Logger logger = LoggerFactory.getLogger(MobileMessage.class);

	public static boolean sendMsg(String phone) {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(Constants.MSERVER_URL);

		String curTime = String.valueOf((new Date().getTime() / 1000L));
		String checkSum = CheckSumBuilder.getCheckSum(Constants.MAPP_SECRET, Constants.MNONCE, curTime);
		logger.info("输入的电话号码为" + phone);
		// 设置请求的header
		post.addHeader("AppKey", Constants.MAPP_KEY);
		post.addHeader("Nonce", Constants.MNONCE);
		post.addHeader("CurTime", curTime);
		post.addHeader("CheckSum", checkSum);
		post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

		// 设置请求参数
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		nameValuePairs.add(new BasicNameValuePair("mobile", phone));
		// nameValuePairs.add(new BasicNameValuePair("templateid",MOULD_ID));
		// nameValuePairs.add(new
		// BasicNameValuePair("mobiles","['"+phone+"']"));
		// nameValuePairs.add(new BasicNameValuePair("params","['"+msg+"']"));
		try {
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
			logger.info("请求参数post"+nameValuePairs);
			// 执行请求
			HttpResponse response = httpclient.execute(post);
			String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");

			org.json.JSONObject object = new org.json.JSONObject(responseEntity);
			logger.info("CHECKCODE RESPONSE"+object);
			// String code= Object.getString("code");
			int code = object.getInt("code");
			if (code == 200) {
				String obj = object.getString("obj");
				logger.info("CEHCKCODE RESPONSE obj" + obj);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	public static boolean checkMsg(String phone, String code) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(Constants.VERIFY_URL);

		String curTime = String.valueOf((new Date().getTime() / 1000L));
		String checkSum = CheckSumBuilder.getCheckSum(Constants.MAPP_SECRET, Constants.MNONCE, curTime);
		logger.info("CHECKCODE TEL "+phone);
		// 设置请求的header
		post.addHeader("AppKey", Constants.MAPP_KEY);
		post.addHeader("Nonce", Constants.MNONCE);
		post.addHeader("CurTime", curTime);
		post.addHeader("CheckSum", checkSum);
		post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

		// 设置请求参数
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		nameValuePairs.add(new BasicNameValuePair("mobile", phone));
		nameValuePairs.add(new BasicNameValuePair("code", code));
		try {
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
			logger.info("CHECKCODE POST PARAMETERS "+nameValuePairs);
			// 执行请求
			HttpResponse response = httpclient.execute(post);
			String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");
			org.json.JSONObject object = new org.json.JSONObject(responseEntity);
			logger.info(object.toString());
			int returnCode = object.getInt("code");
			if (returnCode == 200) {
				logger.info("CHECK IS SUCCESSFUL");
				return true;
			} else {
				return false;
			}
		} catch (Exception e1) {
			logger.info("CHECK CODE EXCEPTION IS HAPPENED");
			e1.printStackTrace();
			return false;
		}
	}

	public static boolean sendMsgReserv(int templateid,String phone, JSONArray params) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(Constants.SENDTAMPLATE_URL);
		String curTime = String.valueOf((new Date().getTime() / 1000L));
		String checkSum = CheckSumBuilder.getCheckSum(Constants.SENDTAMPLATE_APP_SECRET, Constants.MNONCE, curTime);
		logger.info("SEND MSG PHONE "+phone);
		// 设置请求的header
		post.addHeader("AppKey", Constants.SENDTAMPLATE_APP_KEY);
		post.addHeader("Nonce", Constants.MNONCE);
		post.addHeader("CurTime", curTime);
		post.addHeader("CheckSum", checkSum);
		post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		// 设置请求参数
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(phone);
		nameValuePairs.add(new BasicNameValuePair("mobiles", jsonArray.toString()));
		nameValuePairs.add(new BasicNameValuePair("templateid", "" + templateid));
		nameValuePairs.add(new BasicNameValuePair("params", params.toString()));

		// nameValuePairs.add(new
		// BasicNameValuePair("mobiles","['"+phone+"']"));
		// nameValuePairs.add(new BasicNameValuePair("params","['"+msg+"']"));
		try {
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
			logger.info("SEND MSG RESERV POST PARAMTERS"+ nameValuePairs);
			// 执行请求
			HttpResponse response = httpclient.execute(post);
			String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");
			org.json.JSONObject object = new org.json.JSONObject(responseEntity);
			logger.info("SEND MSG RESERV RESPONSE"+object);
			int code = object.getInt("code");
			if (code == 200) {
				int obj = object.getInt("obj");
				logger.info("SEND MSG RESERV RESPONSE OBJ"+obj);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
